var a;
require("../util.js");
require("../component/components.js");
require("../component/seeHeader.js");
require("../component/footer.js");
require("../component/basicSlide.js");
require("../../jsMt/util.js");
// var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#enterpriseAd",
    data:{
        type:1,
        indexs:0,
        isNav:1,
        loadFlag:false,
        isDown:false,
        hours:"",//时
        minutes:"",//分
        seconds:"",//秒
        startsecond:"",
        endSecond:"",
        enterpriseCarouselList:{},//轮播图
        enterpriseCategoryList:{},//分类
        enterpriseHotAdvTypeList:{},//时间
        HotAdvTypeList:[],
        typeId:"1",//热度类型id
        pageNumber:"1",
        enterpriseHotAdvList:{},//热度类型列表
        categoryList:{},
        categoryList1:[],
        categoryList2:[],
        categoryId:"",
        categoryIdName:"",
        sortDate:""
    },
    mounted:function(){
        this.getEnterpriseCarouselList();
        this.getEnterpriseCategoryList();
        this.getEnterpriseHotAdvTypeList();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        getDown:function(){
            this.isDown=!this.isDown;
        },
        //切换
        getTabData:function(num,item){
            this.indexs=num;
            this.typeId=item.typeId;
            this.sortDate=item.sortDate;
            this.pageNumber=1;
            this.enterpriseHotAdvList={};
            this.enterpriseHotAdvList1=[];
            this.enterpriseHotAdvList2=[];
            this.getEnterpriseHotAdvList();
        },
        //企业广告轮播图
        getEnterpriseCarouselList:function(){
            var that=this;
            this.ajax({url:"/see/getEnterpriseCarouselList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseCarouselList=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取轮播失败！")
                }
            });
        },
        //企业广告分类
        getEnterpriseCategoryList:function(){
            var that=this;
            this.ajax({url:"/see/getEnterpriseCategoryList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseCategoryList=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取分类失败！")
                }
            });
        },
        //获取企业广告热度类型列表
        getEnterpriseHotAdvTypeList:function(){
            var that=this;
            this.ajax({url:"/see/getEnterpriseHotAdvTypeList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseHotAdvTypeList=b.json.data;
                    that.HotAdvTypeList=that.enterpriseHotAdvTypeList[0];
                    that.typeId=that.HotAdvTypeList.typeId;
                    that.enterpriseHotAdvTypeList.forEach(function(val){
                        if(val.sortDate==0){
                            // var currentTime=getNowFormatDate()+" "+val.currentTime;
                            // console.log(currentTime)
                            // console.log(val.endDate.split("").length);
                            // var end=getNowFormatDate()+" "+val.endDate;
                            // console.log(end)
                            // countTime(currentTime,end);
                            that.compareDate(val);
                            that.sortDate=val.sortDate;
                        }
                    });
                    console.log(that.enterpriseHotAdvTypeList)
                    that.getEnterpriseHotAdvList();
                }else{
                    vm.$refs.prompt.toast("获取分类失败！")
                }
            });
        },
        compareDate:function(item,typeId){
            var that=this;
            console.log(item)
            var dataList=item.currentTime.split(":");
            var hour,minute,second;
            hour=parseInt(item.endDate)-parseInt(dataList[0]);
            if(dataList[1]>0||dataList[2]>0){
                hour--;
                minute=60-parseInt(dataList[1]);
                if(dataList[2]>0){
                    minute--;
                    second=60-parseInt(dataList[2]);
                }
            }
            console.log(hour+"时"+minute+"分"+second+"秒");
            that.hours=hour;
            that.minutes=minute;
            that.seconds=second;
            if(that.hours>=0&&that.minutes>=0&&that.seconds>0){
                a=setInterval(this.leftTimer,1000);
            }
     /*       else{
                that.hours="00";
                that.minutes="00";
                that.seconds="00";
            }*/
            //this.leftTimer();
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            that.hours=Number(that.hours);
            that.minutes=Number(that.minutes);
            that.seconds=Number(that.seconds);
            if(that.hours==0&&that.minutes==0&&that.seconds==0){
                clearInterval(a);
            }else{
                that.seconds--;
                if(that.seconds == 0){
                    that.minutes--;
                    that.seconds = 59;
                }
                if(that.minutes < 0 && that.hours > 0){
                    that.hours--;
                    that.minutes = 59;
                }
                if (that.hours <= 9) that.hours = '0' + that.hours;
                if (that.minutes <= 9) that.minutes = '0' + that.minutes;
                if (that.seconds <= 9) that.seconds = '0' + that.seconds;
            }
        },
        //获取企业广告热度列表
        getEnterpriseHotAdvList:function(){
            var that=this;
            var data={
                typeId:that.typeId,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/see/getEnterpriseHotAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseHotAdvList=b.json.data;
                    that.enterpriseHotAdvList1=that.enterpriseHotAdvList.list;
                    if(that.enterpriseHotAdvList.pageNumber>1){
                        if(that.enterpriseHotAdvList.list.length>0) {
                            that.enterpriseHotAdvList2 = that.enterpriseHotAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.enterpriseHotAdvList2= that.enterpriseHotAdvList1;
                    }
                    vm.loadFlag=false;
                    lazyload();
                }else{
                    that.enterpriseHotAdvList={};
                    that.enterpriseHotAdvList1=[];
                    that.enterpriseHotAdvList2=[];
                    vm.$refs.prompt.toast("获取列表失败！")
                }
            });
        },
/*        getCategoryList:function(item){
            this.categoryId=item.id;
            this.categoryIdName=item.name;
            this.type=2;
            clearInterval(a);
            this.categoryList={};
            this.categoryList1=[];
            this.categoryList2=[];
            this.pageNumber=1;
            this.getEnterpriseListByCategory();
        },
        //获取企业广告热度列表
        getEnterpriseListByCategory:function(){
            var that=this;
            var data={
                categoryId:that.categoryId,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/see/getEnterpriseListByCategory",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.categoryList=b.json.data;
                    that.categoryList1=that.categoryList.list;
                    if(that.categoryList.pageNumber>1){
                        if(that.categoryList.list.length>0) {
                            that.categoryList2 = that.categoryList2.concat(b.json.data.list);
                        }
                    }else{
                        that.categoryList2= that.categoryList1;
                    }
                    vm.loadFlag=false;
                    lazyload();
                }else{
                    that.categoryList={};
                    that.categoryList1=[];
                    that.categoryList2=[];
                    vm.$refs.prompt.toast("获取列表失败！")
                }
            });
        },*/
        getNext:function(type,item){
            if(type==1){
                location.href="/see/enterpriseAdpage?categoryId="+item.id+"&categoryIdName="+decodeURI(item.name);
            }else if(type==2){
                location.href='/see/adDetails?id='+item.id
                // if(this.sortDate==0){
                //
                // }else{
                //     vm.$refs.prompt.toast("活动未开放！");
                // }
            }
        }
    }
});
function countTime(start,end) {
    //获取当前时间
    var date = new Date(start);
    var now = date.getTime();
    //设置截止时间
    var endDate = new Date(end);
    var end = endDate.getTime();
    console.log(now)
    console.log(end)
    //时间差
    var leftTime = end-now;
    //定义变量 d,h,m,s保存倒计时的时间
    var d,h,m,s;
    if (leftTime>=0) {
        // d = Math.floor(leftTime/1000/60/60/24);
        h = Math.floor(leftTime/1000/60/60%24);
        m = Math.floor(leftTime/1000/60%60);
        s = Math.floor(leftTime/1000%60);
    }

    //递归每秒调用countTime方法，显示动态时间效果
    //setTimeout(countTime,1000);

}
//获取当前时间，格式YYYY-MM-DD
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
var loadFlag;
window.onscroll=function(){
    loadFlag = vm.enterpriseHotAdvList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            if(vm.type==1){
                vm.getEnterpriseHotAdvList();
            }else if(vm.type==2){
                vm.getEnterpriseListByCategory();
            }
            lazyload();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}